package com.eric.rabbitmq.topics;

import com.eric.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class EmitLog {
    public static final String EXCHANGE_NAME = "topic_logs";
    public static final String ROUTING_KEY = "lazy.orange.rabbit";//orange or rabbit lazy

    public static void main(String[] args) throws Exception{
        //声明信道
        Channel channel = RabbitMqUtils.getChannel();
        //声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME,  BuiltinExchangeType.TOPIC);
       //输入消息
/*        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String message = scanner.next();
            channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY,null,message.getBytes("UTF-8"));
            System.out.println("发送消息："+ message);
        }*/
        //定义一个发消息map
        Map<String,String>bindingKeyMap = new HashMap<>();
        bindingKeyMap.put("quick.orange.rabbit","被队列Q1Q2接收到");
        bindingKeyMap.put("lazy.orange.elephant","被队列Q1Q2接收到");
        bindingKeyMap. put("quick.orange.fox","被队列Q1接收到");
        bindingKeyMap.put("lazy.brown.fox","被队列Q2接收到");
        bindingKeyMap.put("lazy.pink.rabbit","虽然满足两个绑定但只被队列Q2接收一次");
        bindingKeyMap. put("quick.brown.fox","不匹配任何绑定不会被任何队列接收到会被丢弃");
        bindingKeyMap. put("quick.orange.male.rabbit","是四个单词不匹配任何绑定会被丢弃");
        bindingKeyMap. put("lazy.orange.male.rabbit","是四个单词但匹配Q2");

        for (Map.Entry<String,String>bindingKeyEntry: bindingKeyMap.entrySet()){
            String routingKey = bindingKeyEntry.getKey();
            String message = bindingKeyEntry.getValue();
            channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes("UTF-8"));
            System.out.println("发出消息："+ message);
        }
    }
}
